﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace Diplomova_prace
{
    /// <summary>
    /// Stav - OK, případně rozšíření TextBox o RichTextBox
    /// </summary>
    static class Extension
    {
        public static int countDecimalPlaces = 5;
        public static string ToDescription(this Enum value)
        {
            var da = (DescriptionAttribute[])(value.GetType().GetField(value.ToString())).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return da.Length > 0 ? da[0].Description : value.ToString();
        }
        public static string MyToString(this double x)
        {
            int significant_digits = countDecimalPlaces;
            if (x.ToString().Length < countDecimalPlaces)
            {
                return x.ToString();
            }
            //Check for special numbers and non-numbers
            if (double.IsInfinity(x) || double.IsNaN(x) || x == 0)
            {
                return x.ToString();
            }
            // extract sign so we deal with positive numbers only
            int sign = Math.Sign(x);
            x = Math.Abs(x);
            // get scientific exponent, 10^3, 10^6, ...
            int sci = (int)Math.Floor(Math.Log(x, 10) / 3) * 3;
            if (Math.Abs(sci) <= 5)
                return x.ToString();
            // scale number to exponent found
            x = x * Math.Pow(10, -sci);
            // find number of digits to the left of the decimal
            int dg = (int)Math.Floor(Math.Log(x, 10)) + 1;
            // adjust decimals to display
            int decimals = Math.Min(significant_digits - dg, 15);
            // format for the decimals
            string fmt = new string('0', decimals);
            if (sci == 0)
            {
                //no exponent
                return string.Format("{0}{1:0." + fmt + "}",
                    sign < 0 ? "-" : string.Empty,
                    Math.Round(x, decimals));
            }
            int index = sci / 3 + 6;
            // with 10^exp format
            return string.Format("{0}{1:0." + fmt + "}E{2}",
                sign < 0 ? "-" : string.Empty,
                Math.Round(x, decimals),
                sci);
        }

        public static IEnumerable<System.Windows.Forms.Control> GetAll(this System.Windows.Forms.Control control, Type type)
        {
            var controls = control.Controls.Cast<System.Windows.Forms.Control>();
            return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                      .Concat(controls)
                                      .Where(c => c.GetType() == type);
        }
        public static void RemoveSpacesFromTextBoxs(this System.Windows.Forms.Control control)
        {
            
            var c = control.GetAll(typeof(TextBox))/*.Concat(control.GetAll(typeof(RichTextBox)))*/;
            List<Control> controls = c.ToList();
            for (int i = 0; i < controls.Count(); i++)
            {
                if (controls[i] is TextBox)
                    controls[i].Text = Regex.Replace(controls[i].Text, @"\s+", "");
            }
        }
        public static void FillListChangeMethod(this System.Windows.Forms.Form form,ListBox ChangeMethod_ListBox,Dictionary<int,string> Methods)
          {
              for (int i = 0; i < Methods.Count; i++)
                  ChangeMethod_ListBox.Items.Add(Methods[i]);
          }
        public static string CheckTextBoxsInfinity(this System.Windows.Forms.Control control,Expression expression)
          {
              var c = control.GetAll(typeof(TextBox));
              List<Control> controls = c.ToList();
              for (int i = 0; i < controls.Count(); i++)
              {
                  if (controls[i] is TextBox)
                      if (controls[i].Text != string.Empty && controls[i].Visible != false && !controls[i].Text.Contains('x')) // například pro nesmyslné meze 
                          if (double.IsInfinity(expression.ComputeRPN(expression.RPN(controls[i].Text))) || double.IsNaN(expression.ComputeRPN(expression.RPN(controls[i].Text))))
                              return controls[i].Tag.ToString();
              }
              return string.Empty;
          }
    }
}
